
package huajiu_com_weixinproject.weixin_project;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author ItBotPjq
 *2018年1月16日
 */
public class test {
	public static void main(String[] args) {
		InputStream is = null;
		String url="http://192.168.1.143:80/hj_message_api/i/test?mid=1&param=22&param1=44&sign=44";
		try {
			 URL urlGet = new URL(url);
	           HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
	           http.setRequestMethod("GET"); // 必须是get方式请求
	           http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
	           http.setDoOutput(true);
	           http.setDoInput(true);
	           System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
	           System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
	           http.connect();
	           // 获取文件转化为byte流
	           is = http.getInputStream();
	           System.out.println(is);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		byte[] data = new byte[1024];
	    
        int len = 0;
        FileOutputStream fileOutputStream = null;

        try {
    		File file = new File("/home/wechat/image/" + "test"); 
    		System.out.println(file.getAbsolutePath());
    		File parent = file.getParentFile(); 
    		if(!parent.exists()){ 
    			parent.mkdirs(); 
    		} 
        	if(!file.exists()) {
        		file.createNewFile();
        	}
/*	        	System.out.println(file.getAbsolutePath());*/
            fileOutputStream = new FileOutputStream(file);
            while ((len = is.read(data)) != -1) {
                fileOutputStream.write(data, 0, len);
/*                String message = new String(data, "UTF-8");
                System.out.println(message);*/
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
	}

}
